﻿using HoverTree.IDAL;
using HoverTree.Model;
using System.Data;
using System.Data.SqlClient;
using HoverTree.DBTools;
using System;
using System.Collections.Generic;

namespace HoverTree.SQLServerDAL
{
    public class HvtPanelUser : IPanelUser
    {
        public string Add(PanelUserInfo panelUser)
        {
            IDataParameter[] m_parameters = new SqlParameter[3];

            m_parameters[0] = new SqlParameter("@hvtusername", SqlDbType.NVarChar, 20);
            m_parameters[1] = new SqlParameter("@hvtpassword", SqlDbType.NVarChar, 50);
            m_parameters[2] = new SqlParameter("@hvtwelcome", SqlDbType.NVarChar, 200);

            m_parameters[0].Value = panelUser.Hvtusername;
            m_parameters[1].Value =  panelUser.Hvtpassword;
            m_parameters[2].Value = panelUser.HvtWelcome;

            int m_returnCount;

            //执行结果等于1表示添加成功
            m_returnCount = KeleyiSQLHelper.RunProcedureWithReturn("Sp_HoverTree_PanelUser_Add", m_parameters);

            if (m_returnCount == 1)
                return "";
            else
                return "添加失败 ";
        }

        /// <summary>
        /// 修改欢迎语
        /// </summary>
        /// <param name="panelUser"></param>
        /// <returns></returns>
        public string Alter(PanelUserInfo panelUser)
        {
            IDataParameter[] m_parameters = new SqlParameter[2];
            m_parameters[0] = new SqlParameter("@hvtid", SqlDbType.Int);
            m_parameters[1] = new SqlParameter("@hvtwelcome", SqlDbType.NVarChar, 200);

            m_parameters[0].Value = panelUser.HvtId;
            m_parameters[1].Value = panelUser.HvtWelcome;

            int m_returnCount;

            //执行结果等于1表示添加成功
            m_returnCount = KeleyiSQLHelper.RunProcedureWithReturn("Sp_HoverTree_PanelUser_Alter", m_parameters);

            if (m_returnCount == 1)
                return "";
            else
                return "修改失败 ";
        }

        public string AlterPassword(PanelUserInfo panelUser)
        {
            if (panelUser.Hvtusername.Contains("'"))
                return "修改失败";

            IDataParameter[] m_parameters = new SqlParameter[2];
            m_parameters[0] = new SqlParameter("@hvtusername", SqlDbType.NVarChar,20);
            m_parameters[1] = new SqlParameter("@hvtpassword", SqlDbType.NVarChar, 50);

            m_parameters[0].Value = panelUser.Hvtusername;
            m_parameters[1].Value = panelUser.Hvtpassword;

            int m_returnCount;

            //执行结果等于1表示添加成功
            m_returnCount = KeleyiSQLHelper.RunProcedureWithReturn("Sp_HoverTree_PanelUser_AlterPassword", m_parameters);

            if (m_returnCount == 1)
                return "";
            else
                return "修改失败 ";

        }

        public string Delete(int hvtid)
        {
            IDataParameter[] m_parameters = new SqlParameter[1];
            m_parameters[0] = new SqlParameter("@hvtID", SqlDbType.Int);
            m_parameters[0].Value = hvtid;

            int m_returnCount;

            //执行结果等于1表示添加成功
            m_returnCount = KeleyiSQLHelper.RunProcedureWithReturn("Sp_HoverTree_PanelUser_Delete", m_parameters);

            if (m_returnCount == 1)
                return "";
            else
                return "删除失败 ";
        }

        public PanelUserInfo Get(int hvtID)
        {
            IDataParameter[] m_parameters = new SqlParameter[1];
            m_parameters[0] = new SqlParameter("@hvtID", SqlDbType.Int);
            m_parameters[0].Value = hvtID;

            DataRow m_dr;
            m_dr = KeleyiSQLHelper.RunProcedureDataRow("Sp_HoverTree_PanelUser_GET", m_parameters);

            if (m_dr == null)
                return null;

            return ConvertDataRowToInfo(m_dr);
        }

        public PanelUserInfo GetByUserName(string userName)
        {
            if (userName.Contains("'"))
                return null;

            IDataParameter[] m_parameters = new SqlParameter[1];
            m_parameters[0] = new SqlParameter("@hvtusername", SqlDbType.NVarChar,20);
            m_parameters[0].Value = userName;

            DataRow m_dr;
            m_dr = KeleyiSQLHelper.RunProcedureDataRow("Sp_HoverTree_PanelUser_GETByUsername", m_parameters);

            if (m_dr == null)
                return null;

            return ConvertDataRowToInfo(m_dr);
        }

        public List<PanelUserInfo> HvtPageQuery(int sortType, int pageSize, int pageIndex, out int totalCount, out int totalPageCount)
        {
            DataTable m_dt = KeleyiSQLHelper.HvtPageQuery("hove" + "rtree_paneluser", "*", "hvtid", " hvtid>0 "
                , "  hvtId desc  ", sortType, pageSize, pageIndex, 0, out totalCount, out totalPageCount);

            return ConvertToList(m_dt);
        }

        PanelUserInfo ConvertDataRowToInfo(DataRow row)
        {
            PanelUserInfo m_info = new PanelUserInfo();
            m_info.HvtId =Convert.ToInt32( row["hvtid"]);
            m_info.Hvtusername = row["hvtusername"].ToString();
            m_info.Hvtpassword = row["hvtpassword"].ToString();
            m_info.HvtWelcome = row["hvtwelcome"].ToString();

            return m_info;
        }

        List<PanelUserInfo> ConvertToList(DataTable table)
        {
            List<PanelUserInfo> m_list = new List<PanelUserInfo>();
            if (table == null)
                return null;
            int m_rows = table.Rows.Count;
            if (m_rows == 0)
                return m_list;


            for (int i = 0; i < m_rows; i++)
            {
                PanelUserInfo m_info = new PanelUserInfo();

                m_info= ConvertDataRowToInfo(table.Rows[i]);
                m_list.Add(m_info);
            }

            return m_list;
        }
    }
}